package com.ym521.imgcrop.crop.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ym521.imgcrop.R;
import com.ym521.imgcrop.widget.RectCropView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ym
 * E-mail: 2435970206@qq.com
 * createTime:2024/5/22
 * explain:
 */
public class SelectAdapter extends RecyclerView.Adapter<SelectViewHolder> {

    private List<RectCropView.WHRatio> dataList = new ArrayList<>();

    private OnItemClickListener clickListener;

    public void setClickListener(OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void initDataList(List<RectCropView.WHRatio> list) {
        dataList.clear();
        dataList.addAll(list);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public SelectViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.select_rectcrop_item, parent, false);
        return new SelectViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SelectViewHolder holder, int position) {
        RectCropView.WHRatio ratio = dataList.get(position);
        holder.showView(ratio);
        holder.itemView.setOnClickListener(v -> {
            if (clickListener != null) {
                clickListener.onClick(ratio, holder.tvContent.getText().toString().trim());
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }


    public interface OnItemClickListener {

        void onClick(RectCropView.WHRatio ratio, String content);
    }

}
